Check CUDA configs¶
In [ ]:
import ultralytics
import torch
# %export CUDA_VISIBLE_DEVICES=""
# torch.cuda.is_available()
# torch.cuda.set_device(0)
In [ ]:
# check cuda configurations
print(torch.version.cuda)
# torch.cpu()
print(torch.cuda.is_available())
print(torch.cuda.current_device())
print(torch.cuda.get_device_name(0))
12.1 True 0 NVIDIA GeForce RTX 4070 Laptop GPU
In [ ]:
ultralytics.checks()
!nvidia-smi
Ultralytics YOLOv8.0.175 Python-3.11.5 torch-2.1.0+cu121 CUDA:0 (NVIDIA GeForce RTX 4070 Laptop GPU, 8188MiB) Setup complete (32 CPUs, 15.7 GB RAM, 280.1/634.8 GB disk)
Fri Oct 27 23:11:07 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 537.34 Driver Version: 537.34 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 4070 ... WDDM | 00000000:01:00.0 On | N/A |
| N/A 40C P0 12W / 140W | 492MiB / 8188MiB | 3% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 2092 C+G ...Programs\Microsoft VS Code\Code.exe N/A |
| 0 N/A N/A 8528 C+G C:\Windows\explorer.exe N/A |
| 0 N/A N/A 10680 C+G ...2txyewy\StartMenuExperienceHost.exe N/A |
| 0 N/A N/A 10856 C+G ...nt.CBS_cw5n1h2txyewy\SearchHost.exe N/A |
| 0 N/A N/A 12928 C+G ...t.LockApp_cw5n1h2txyewy\LockApp.exe N/A |
| 0 N/A N/A 13384 C+G ...GeForce Experience\NVIDIA Share.exe N/A |
| 0 N/A N/A 16776 C+G ...5n1h2txyewy\ShellExperienceHost.exe N/A |
| 0 N/A N/A 17760 C+G ...CBS_cw5n1h2txyewy\TextInputHost.exe N/A |
+---------------------------------------------------------------------------------------+
MODEL TRAINING¶
In [ ]:
from ultralytics import YOLO
import os
import matplotlib.pyplot as plt
import cv2
In [ ]:
model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
data = os.getcwd() + "/datasets/EE297_Project.v4i.yolov8/data.yaml"
epochs = 10
imgsz = 640
batch = 16
device = 0
results = model.train(
data=data, epochs=epochs, imgsz=imgsz, batch=16, device="cpu", workers=1
)
New https://pypi.org/project/ultralytics/8.0.202 available Update with 'pip install -U ultralytics' Ultralytics YOLOv8.0.175 Python-3.11.5 torch-2.1.0+cu121 CPU (13th Gen Intel Core(TM) i9-13900HX) engine\trainer: task=detect, mode=train, model=yolov8n.pt, data=d:\Programming\workspace\plastic-detection/datasets/EE297_Project.v4i.yolov8/data.yaml, epochs=10, patience=50, batch=16, imgsz=640, save=True, save_period=-1, cache=False, device=cpu, workers=1, project=None, name=None, exist_ok=False, pretrained=True, optimizer=auto, verbose=True, seed=0, deterministic=True, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, resume=False, amp=True, fraction=1.0, profile=False, freeze=None, overlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, save_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, half=False, dnn=False, plots=True, source=None, show=False, save_txt=False, save_conf=False, save_crop=False, show_labels=True, show_conf=True, vid_stride=1, stream_buffer=False, line_width=None, visualize=False, augment=False, agnostic_nms=False, classes=None, retina_masks=False, boxes=True, format=torchscript, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=None, workspace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, cfg=None, tracker=botsort.yaml, save_dir=runs\detect\train19 Overriding model.yaml nc=80 with nc=5 from n params module arguments 0 -1 1 464 ultralytics.nn.modules.conv.Conv [3, 16, 3, 2] 1 -1 1 4672 ultralytics.nn.modules.conv.Conv [16, 32, 3, 2] 2 -1 1 7360 ultralytics.nn.modules.block.C2f [32, 32, 1, True] 3 -1 1 18560 ultralytics.nn.modules.conv.Conv [32, 64, 3, 2] 4 -1 2 49664 ultralytics.nn.modules.block.C2f [64, 64, 2, True] 5 -1 1 73984 ultralytics.nn.modules.conv.Conv [64, 128, 3, 2] 6 -1 2 197632 ultralytics.nn.modules.block.C2f [128, 128, 2, True] 7 -1 1 295424 ultralytics.nn.modules.conv.Conv [128, 256, 3, 2] 8 -1 1 460288 ultralytics.nn.modules.block.C2f [256, 256, 1, True] 9 -1 1 164608 ultralytics.nn.modules.block.SPPF [256, 256, 5] 10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 11 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1] 12 -1 1 148224 ultralytics.nn.modules.block.C2f [384, 128, 1] 13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 14 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1] 15 -1 1 37248 ultralytics.nn.modules.block.C2f [192, 64, 1] 16 -1 1 36992 ultralytics.nn.modules.conv.Conv [64, 64, 3, 2] 17 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1] 18 -1 1 123648 ultralytics.nn.modules.block.C2f [192, 128, 1] 19 -1 1 147712 ultralytics.nn.modules.conv.Conv [128, 128, 3, 2] 20 [-1, 9] 1 0 ultralytics.nn.modules.conv.Concat [1] 21 -1 1 493056 ultralytics.nn.modules.block.C2f [384, 256, 1] 22 [15, 18, 21] 1 752287 ultralytics.nn.modules.head.Detect [5, [64, 128, 256]] Model summary: 225 layers, 3011823 parameters, 3011807 gradients Transferred 319/355 items from pretrained weights TensorBoard: Start with 'tensorboard --logdir runs\detect\train19', view at http://localhost:6006/ Freezing layer 'model.22.dfl.conv.weight' train: Scanning D:\Programming\workspace\plastic-detection\datasets\EE297_Project.v4i.yolov8\train\labels.cache... 1857 images, 3 backgrounds, 0 corrupt: 100%|██████████| 1857/1857 [00:00<?, ?it/s] WARNING Box and segment counts should be equal, but got len(segments) = 324, len(boxes) = 2055. To resolve this only boxes will be used and all segments will be removed. To avoid this please supply either a detect or segment dataset, not a detect-segment mixed dataset. val: Scanning D:\Programming\workspace\plastic-detection\datasets\EE297_Project.v4i.yolov8\valid\labels.cache... 491 images, 4 backgrounds, 0 corrupt: 100%|██████████| 491/491 [00:00<?, ?it/s] WARNING Box and segment counts should be equal, but got len(segments) = 86, len(boxes) = 540. To resolve this only boxes will be used and all segments will be removed. To avoid this please supply either a detect or segment dataset, not a detect-segment mixed dataset. Plotting labels to runs\detect\train19\labels.jpg... optimizer: 'optimizer=auto' found, ignoring 'lr0=0.01' and 'momentum=0.937' and determining best 'optimizer', 'lr0' and 'momentum' automatically... optimizer: AdamW(lr=0.001111, momentum=0.9) with parameter groups 57 weight(decay=0.0), 64 weight(decay=0.0005), 63 bias(decay=0.0) Image sizes 640 train, 640 val Using 0 dataloader workers Logging results to runs\detect\train19 Starting training for 10 epochs... Closing dataloader mosaic Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 1/10 0G 0.7891 3.125 1.442 1 640: 100%|██████████| 117/117 [05:17<00:00, 2.71s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:29<00:00, 1.86s/it] all 491 540 0.357 0.51 0.396 0.285 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 2/10 0G 0.7997 2.41 1.46 1 640: 100%|██████████| 117/117 [04:51<00:00, 2.49s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:25<00:00, 1.60s/it] all 491 540 0.556 0.382 0.381 0.277 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 3/10 0G 0.8247 2.088 1.459 2 640: 100%|██████████| 117/117 [04:59<00:00, 2.56s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:28<00:00, 1.81s/it] all 491 540 0.301 0.386 0.256 0.145 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 4/10 0G 0.7964 1.796 1.425 1 640: 100%|██████████| 117/117 [04:27<00:00, 2.28s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:24<00:00, 1.52s/it] all 491 540 0.58 0.535 0.571 0.429 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 5/10 0G 0.7265 1.591 1.38 1 640: 100%|██████████| 117/117 [04:03<00:00, 2.08s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:24<00:00, 1.52s/it] all 491 540 0.551 0.628 0.617 0.479 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 6/10 0G 0.705 1.429 1.362 1 640: 100%|██████████| 117/117 [04:03<00:00, 2.08s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:24<00:00, 1.51s/it] all 491 540 0.665 0.651 0.701 0.544 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 7/10 0G 0.6887 1.312 1.325 1 640: 100%|██████████| 117/117 [04:04<00:00, 2.09s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:24<00:00, 1.51s/it] all 491 540 0.648 0.656 0.689 0.563 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 8/10 0G 0.6316 1.199 1.277 1 640: 100%|██████████| 117/117 [04:03<00:00, 2.09s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:23<00:00, 1.49s/it] all 491 540 0.699 0.632 0.711 0.574 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 9/10 0G 0.5917 1.087 1.239 1 640: 100%|██████████| 117/117 [04:05<00:00, 2.09s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:24<00:00, 1.50s/it] all 491 540 0.72 0.702 0.755 0.632 Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size 10/10 0G 0.5811 1.048 1.238 1 640: 100%|██████████| 117/117 [04:04<00:00, 2.09s/it] Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:23<00:00, 1.50s/it] all 491 540 0.77 0.725 0.788 0.652 10 epochs completed in 0.805 hours. Optimizer stripped from runs\detect\train19\weights\last.pt, 6.2MB Optimizer stripped from runs\detect\train19\weights\best.pt, 6.2MB Validating runs\detect\train19\weights\best.pt... Ultralytics YOLOv8.0.175 Python-3.11.5 torch-2.1.0+cu121 CPU (13th Gen Intel Core(TM) i9-13900HX) Model summary (fused): 168 layers, 3006623 parameters, 0 gradients Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 16/16 [00:19<00:00, 1.21s/it] all 491 540 0.771 0.722 0.787 0.652 Cardboard 491 72 0.914 0.736 0.833 0.745 Glass 491 90 0.811 0.811 0.841 0.654 Metal 491 70 0.823 0.531 0.724 0.617 Paper 491 157 0.662 0.732 0.754 0.638 Plastic 491 151 0.646 0.798 0.785 0.604 Speed: 0.9ms preprocess, 35.0ms inference, 0.0ms loss, 0.3ms postprocess per image Results saved to runs\detect\train19
MODEL COMPARISON¶
In [ ]:
import cv2
import os
import matplotlib.pyplot as plt
from ultralytics import YOLO
pretrained (default)
In [ ]:
def test_model(model):
test_imgs = [
"/datasets/EE297_Project.v4i.yolov8/test/images/PXL_20220330_091951914_jpg.rf.2be1c42071953a90ce2781c2d0d18d8c.jpg",
"/datasets/EE297_Project.v4i.yolov8/test/images/trash83_jpg.rf.aa417b0b095f4babf254cda7c56710d7.JPG",
"/datasets/EE297_Project.v4i.yolov8/test/images/metal242_jpg.rf.9e181d3e1d9b561a79e4fd0a8d747a2e.jpg",
"/datasets/EE297_Project.v4i.yolov8/test/images/20220406_110922_jpg.rf.e08d550fbc12079e7723725ebc7223ce.jpg",
"/datasets/classification_dataset/images/IMG_0961.jpeg",
"/datasets/classification_dataset/images/IMG_0975.jpeg",
"/datasets/classification_dataset/images/IMG_1414.jpeg",
"/datasets/EE297_Project.v4i.yolov8/test/images/20220406_111424_jpg.rf.412e398d3763d5c122b86b0d3f07e505.jpg",
"/datasets/EE297_Project.v4i.yolov8/test/images/PXL_20220330_090752236_jpg.rf.1191ca3a67a9f20d7629428a0cc4b43e.jpg",
]
for img_path in test_imgs:
img = cv2.imread(os.getcwd() + img_path)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
results = model.predict(img)
plt.imshow(cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB))
plt.show()
model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training)
test_model(model)
0: 640x640 (no detections), 5.5ms Speed: 1.0ms preprocess, 5.5ms inference, 1.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 1 kite, 5.0ms Speed: 3.0ms preprocess, 5.0ms inference, 1.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 1 kite, 5.0ms Speed: 2.0ms preprocess, 5.0ms inference, 1.5ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 1 handbag, 4.5ms Speed: 3.0ms preprocess, 4.5ms inference, 3.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x480 1 umbrella, 5.5ms Speed: 2.0ms preprocess, 5.5ms inference, 2.0ms postprocess per image at shape (1, 3, 640, 480)
0: 640x480 (no detections), 16.6ms Speed: 2.1ms preprocess, 16.6ms inference, 1.0ms postprocess per image at shape (1, 3, 640, 480)
0: 640x480 1 bench, 1 bottle, 1 potted plant, 11.8ms Speed: 2.0ms preprocess, 11.8ms inference, 6.4ms postprocess per image at shape (1, 3, 640, 480)
0: 640x640 1 person, 17.0ms Speed: 3.0ms preprocess, 17.0ms inference, 6.5ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 (no detections), 18.5ms Speed: 3.0ms preprocess, 18.5ms inference, 1.0ms postprocess per image at shape (1, 3, 640, 640)
test trained model
In [ ]:
model_path = "runs/detect/train/weights/best.pt"
# trained_model = YOLO("yolov8n.yaml").load(model_path)
trained_model = YOLO(model_path)
test_model(trained_model)
0: 640x640 1 Plastic, 16.1ms Speed: 3.9ms preprocess, 16.1ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 1 Plastic, 15.1ms Speed: 3.5ms preprocess, 15.1ms inference, 6.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 1 Metal, 11.5ms Speed: 3.2ms preprocess, 11.5ms inference, 5.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 1 Plastic, 18.8ms Speed: 3.0ms preprocess, 18.8ms inference, 4.5ms postprocess per image at shape (1, 3, 640, 640)
0: 640x480 4 Plastics, 18.1ms Speed: 2.0ms preprocess, 18.1ms inference, 5.2ms postprocess per image at shape (1, 3, 640, 480)
0: 640x480 2 Plastics, 11.2ms Speed: 4.1ms preprocess, 11.2ms inference, 6.1ms postprocess per image at shape (1, 3, 640, 480)
0: 640x480 1 Metal, 4 Plastics, 6.0ms Speed: 2.0ms preprocess, 6.0ms inference, 2.4ms postprocess per image at shape (1, 3, 640, 480)
0: 640x640 1 Paper, 1 Plastic, 9.5ms Speed: 3.0ms preprocess, 9.5ms inference, 1.0ms postprocess per image at shape (1, 3, 640, 640)
0: 640x640 2 Plastics, 20.6ms Speed: 4.0ms preprocess, 20.6ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)